#ifndef PRESETPOSITIONMANAGER_H
#define PRESETPOSITIONMANAGER_H

#include <QWidget>
#include <QList>
#include <QListWidget>
#include <QPushButton>
#include <QMessageBox>
#include <QFileDialog>
#include <QTextStream>
#include <QGroupBox>
#include <QDoubleSpinBox>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <memory>

class Device_Motor; // 前向声明

struct PresetPosition {
    QString name;
    double position;
    QColor color;
    int index;

    PresetPosition(QString n = "", double p = 0.0, QColor c = Qt::white, int i = 0)
        : name(n), position(p), color(c), index(i) {}
};

class PresetPositionManager : public QWidget {
    Q_OBJECT

public:

    explicit PresetPositionManager(std::shared_ptr<Device_Motor> motor, QWidget *parent = nullptr);
    ~PresetPositionManager();

    void setupUI();
    void addPreset(const QString &name, double position);
    void removePreset(int index);
    void moveToPreset(int index);
    void clearPresets();
    void loadFromFile(const QString &path);
    void saveToFile(const QString &path);
    void moveAllPresetsSequentially(bool IsMeasurement=false);
    int getPresetCount() const { return m_presetPositions.size(); }

    QList<PresetPosition> getPresetPositions() const { return m_presetPositions; }

signals:
    void requestMoveToPreset(double position,bool isMeasurement);


private slots:
    void onAddPresetClicked();
    void onRemovePresetClicked();
    void onMoveToPresetClicked();
    void onClearPresetsClicked();
    void onLoadFromFileClicked();
    void onSaveToFileClicked();
    void onPresetDoubleClicked(QListWidgetItem *item);
    void onMoveUpPresetClicked();
    void onMoveDownPresetClicked();
    void onSequentialMoveClicked();


private:
    std::shared_ptr<Device_Motor> m_motor;
    QList<PresetPosition> m_presetPositions;

    // UI组件
    QDoubleSpinBox *m_presetPositionSpinBox;
    QLineEdit *m_presetNameEdit;
    QListWidget *m_presetListView;


    // 按钮
    QPushButton *m_addButton;
    QPushButton *m_removeButton;
    QPushButton *m_moveToButton;
    QPushButton *m_moveUpButton;
    QPushButton *m_moveDownButton;
    QPushButton *m_clearButton;
    QPushButton *m_loadButton;
    QPushButton *m_saveButton;
    QPushButton *m_sequentialMoveButton;
    QCheckBox *m_isMeasurementCheckBox;

    bool m_isSequentialMoving = false;
    int m_currentPresetIndex = 0;
    bool m_isMeasurement = false;
};

#endif